package service

import (
	"io/ioutil"
	"workflow/config"
	"workflow/entity"

	"github.com/gin-gonic/gin"
)

func WebTrigger(c *gin.Context, workflow_id string) (trigger *entity.HisotryTrigger, err error) {
	body, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		return nil, err
	}

	trigger = &entity.HisotryTrigger{
		Type:       "web",
		Raw:        string(body),
		WorkflowID: workflow_id,
	}
	err = config.DB.Save(trigger).Error
	if err != nil {
		return nil, err
	}
	return trigger, nil
}

func TickerTrigger(workflow_id string) (trigger *entity.HisotryTrigger, err error) {
	trigger = &entity.HisotryTrigger{
		Type:       "ticker",
		Raw:        "",
		WorkflowID: workflow_id,
	}
	err = config.DB.Save(trigger).Error
	if err != nil {
		return nil, err
	}
	return trigger, nil
}
